<template>
  <el-dialog
    title="分配角色"
    :visible.sync="showRoleDialog"
    @close="btnCancel"
  >
    <!-- 多选框组 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <!-- 定义footer插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确 定</el-button>
        <el-button size="small" @click="btnCancel">取 消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'

export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      default: '',
      required: true
    }
  },
  data() {
    return {
      list: [], // 存储所有角色
      roleIds: [] // 存储所拥有的角色

    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows } = await getRoleList({ page: 1, pagesize: 20 })
      this.list = rows
    },
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id)
      this.roleIds = roleIds
    },
    async btnOK() {
      // console.log({ id: this.userId, roleIds: this.roleIds })
      const res = await assignRoles({ id: this.userId, roleIds: this.roleIds })
      console.log(res)
      this.$emit('update:showRoleDialog', false)
      // this.$parent.showRoleDialog = false
    },
    async btnCancel() {
      this.roleIds = []
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

<style>

</style>
